package org.onion.pattern.event.springboot.rocketmq.consume;

import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.Nullable;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TopicAndEventTypeMapping {

    private static final Map<String, Class<?>> topicModelRecord = new ConcurrentHashMap<>();

    public void create(String topic, String tags, Class<?> clazz) {
        if (StringUtils.isEmpty(tags) || "*".equals(tags)) {
            topicModelRecord.put(topic, clazz);
        } else {
            topicModelRecord.put(topic + "&" + tags, clazz);
        }
    }

    @Nullable
    public Class<?> getEventType(String topic, String tags) {

        Class<?> eventType = topicModelRecord.get(topic + "&" + tags);
        if (eventType == null) {
            eventType = topicModelRecord.get(topic);
        }
        return eventType;

    }
}
